package com.xingchi.tornado.exception;

import com.xingchi.tornado.basic.BaseCode;

/**
 * 业务异常类
 *
 * @author xingchi
 * @date 2025/06/07 15:30
 */
public class BizException extends UncheckedException {

    private Object data;

    public BizException(BaseCode baseCode) {
        super(baseCode.getCode(), baseCode.getMessage());
    }

    public BizException(BaseCode baseCode, String message) {
        super(baseCode.getCode(), message);
    }

    public BizException(BaseCode baseCode, String message, Object data) {
        super(baseCode.getCode(), message);
        this.data = data;
    }

    public BizException(BaseCode baseCode, Throwable cause) {
        super(baseCode.getCode(), baseCode.getMessage(), cause);
    }

    public BizException(BaseCode baseCode, String message, Throwable cause) {
        super(baseCode.getCode(), message, cause);
    }

    public BizException(BaseCode baseCode, String message, Object data, Throwable cause) {
        super(baseCode.getCode(), message, cause);
        this.data = data;
    }

    /**
     * 静态工厂方法，快速创建异常
     */
    public static BizException of(BaseCode baseCode) {
        return new BizException(baseCode);
    }

    /**
     * 静态工厂方法，使用自定义消息
     */
    public static BizException of(BaseCode baseCode, String message) {
        return new BizException(baseCode, message);
    }

    /**
     * 静态工厂方法，包含原始异常
     */
    public static BizException of(BaseCode baseCode, Throwable cause) {
        return new BizException(baseCode, cause);
    }

    /**
     * 抛出异常的快捷方法
     */
    public static void throwOf(BaseCode baseCode) {
        throw new BizException(baseCode);
    }

    /**
     * 抛出异常的快捷方法，使用自定义消息
     */
    public static void throwOf(BaseCode baseCode, String message) {
        throw new BizException(baseCode, message);
    }

    /**
     * 抛出异常的快捷方法，使用自定义消息
     */
    public static void throwOf(BaseCode baseCode, String message, Object data) {
        throw new BizException(baseCode, message, data);
    }

    /**
     * 根据条件抛出异常
     */
    public static void throwIf(boolean condition, BaseCode baseCode) {
        if (condition) {
            throw new BizException(baseCode);
        }
    }

    /**
     * 根据条件抛出异常，使用自定义消息
     */
    public static void throwIf(boolean condition, BaseCode baseCode, String message) {
        if (condition) {
            throw new BizException(baseCode, message);
        }
    }

    /**
     * 根据条件抛出异常，使用自定义消息
     */
    public static void throwIf(boolean condition, BaseCode baseCode, String message, Object data) {
        if (condition) {
            throw new BizException(baseCode, message, data);
        }
    }

    public Object getData() {
        return data;
    }
}
